﻿using AutoFlatnessTest.Db;
using AutoFlatnessTest.Plc;
using AutoFlatnessTest.ViewModel;
using Mapster;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AutoFlatnessTest
{
    /// <summary>
    /// UcCheck.xaml 的交互逻辑
    /// </summary>
    public partial class UcCheck : UserControl
    {
        UcCheckVModel VModel = new UcCheckVModel();
        public UcCheck()
        {
            InitializeComponent();

            this.DataContext = VModel;
        }

        private void UserControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            VModel.Yz1 = 12;
            if ((bool)e.NewValue == true && !WpfHelp.IsInDesignMode(this))
            {
                var data = DotData.GetDotData();
                VModel.Yz1 = data.Yz1;
                VModel.Smi1 = data.Smi1;
                VModel.Yz2 = data.Yz2;
                VModel.Smi2 = data.Smi2;
                VModel.Yz3 = data.Yz3;
                VModel.Smi3 = data.Smi3;
                VModel.Yz4 = data.Yz4;
                VModel.Smi4 = data.Smi4;
                VModel.Yz5 = data.Yz5;
                VModel.Smi5 = data.Smi5;
                VModel.Ydjl = data.Ydjl;
            }
        }

        private void click_bc(object sender, RoutedEventArgs e)
        {
            try
            {
                DotData dotData = VModel.Adapt<DotData>();
                dotData.SaveData();

                MessageBox.Show("保存成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存失败：" + ex.Message);
            }
        }

        private void click_zx(object sender, RoutedEventArgs e)
        {
            Int16 tag = 0;
            foreach (var item in buttonGroup.Items)
            {
                if (item is RadioButton radioButton)
                {
                    if (radioButton.IsChecked == true)
                    {
                        tag = Convert.ToInt16(radioButton.Tag);
                        break;
                    }
                }
            }

            Task.Run(() =>
            {
                VModel.IsZhiXing = false;

                try
                {
                    InovanceRun.Plc?.SetData<Int16>(InovanceComm.模式, 1);

                    var dotData = DotData.GetDotData();
                    InovanceRun.LoadData(dotData);

                    InovanceRun.Plc.SetData<Int16>(InovanceComm.校准序号, tag);
                    InovanceRun.Plc.SetData<Int16>(InovanceComm.校准移动到待扫描位, 2);

                    VModel.ReData = "准备好后请按下操作台右下绿色启动按钮...";
                    while (true)
                    {
                        if (InovanceRun.Plc.GetData<Int16>(InovanceComm.校准模式测试准备好) == 2)
                            break;

                        Thread.Sleep(300);
                    }

                    //扫描
                    var mi = JiEnShiRun.DeviceSmi.SendWait(JiEnShiRun.SmiGo);
                    //开始移动
                    InovanceRun.Plc.SetData<Int16>(InovanceComm.校准测试开始, 2);
                    Thread.Sleep(500);
                    //扫描数据 94.118,+00000.000,+00000.000
                    var midata = JiEnShiRun.DeviceSmi.Recmes();

                    //如果没有接收到返回值 就再次等待
                    if (string.IsNullOrWhiteSpace(midata) || midata.Contains("T1"))
                        //扫描数据 94.118,+00000.000,+00000.000
                        midata = JiEnShiRun.DeviceSmi.Recmes();

                    var maobj = MiData.GetMiData(midata);

                    VModel.IsZhiXing = true;

                    if (maobj.IsNull)
                    {
                        VModel.ReData = $@"{tag}号（无效的结果）";
                        VModel.ReDatas = $@"{VModel.ReData}
{VModel.ReDatas}";
                    }
                    else
                    {
                        VModel.ReData = $@"{tag}号（差值：{maobj.DiffVal}，最大值：{maobj.MaxVal}，最小值：{maobj.MinVal}）";
                        //VModel.ReData = $@"{tag}号（相似度：{maobj.Simi}，差值：{maobj.DiffVal}，最大值：{maobj.MaxVal}，最小值：{maobj.MinVal}，原始信息：{midata.Trim()}）";
                        VModel.ReDatas = $@"{VModel.ReData}
{VModel.ReDatas}";
                    }

                    DemaRecordDb.Add(new DemaRecord()
                    {
                        TestNum = tag,
                        IsOk = maobj.IsNull ? 2 : 1,
                        MiSimi = maobj.Simi,
                        MiMaxVal = maobj.MaxVal,
                        MiMinVal = maobj.MinVal,
                        MiVal = maobj.DiffVal,
                        MiInfo = midata.Trim(),
                    });
                    InovanceRun.Plc.SetData<Int16>(InovanceComm.校准测试完成, 2);

                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误：" + ex.Message);

                    VModel.IsZhiXing = true;
                }
            });

        }

    }
}
